package cn.sdhuijin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import cn.sdhuijin.service.TargetService;
import cn.sdhuijin.utils.ReadExcel;

/**
 * @author sunp@sdhuijin.cn:
 * @version 创建时间：2015年5月7日 下午3:58:11 类说明
 */

@Controller
public class TargetController {

	@Inject
	private TargetService objectService;

	@RequestMapping(value = { "/objectupload" }, method = RequestMethod.POST)
	@ResponseBody
	public void uploadExcel(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// 转型为MultipartHttpRequest：
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		// 获得文件：
		MultipartFile file = multipartRequest.getFile("uploadfile");

		List<List<Object>> list = ReadExcel.readExcel(file, 0, 2);
		response.setContentType("text/html");
		String result = "";
		if (list.get(0).size() >= 2) {
			try {
				objectService.BatchImport(list);
				result = "{success:true,message:'导入成功'}";
			} catch (NumberFormatException e) {
				result = "{success:false,message:'" + e.getMessage() + "'}";
			} catch (Exception e) {
				result = "{success:false,message:'导入失败！  " + e.getMessage()
						+ "'}";
				e.printStackTrace();
			}
		} else {
			result = "{success:false,message:'导入格式不正确，请您按照模板导入数据'}";
		}
		response.getWriter().write(result);
	}

	@RequestMapping(value = { "/objectrankings" }, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> objectRankings() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		Long[] xId = { 1004L, 1005L, 1006L, 1007L, 1008L, 1009L, 1010L, 1011L,
				1012L, 1013L, 1014L, 1015L, 1016L, 1017L, 1018L };
		int[] frontMonth = { 45, 52, 54, 74, 90, 84, 45, 52, 54, 74, 90, 84,
				45, 52, 54 };
		int[] currentMonth = { 60, 80, 90, 88, 78, 90, 80, 85, 95, 98, 90, 80,
				65, 80, 85 };
		String[] xName = { "高新大队", "邹城大队", "兖州大队", "泗水大队", "汶上大队", "梁山大队",
				"邹城大队", "兖州大队", "泗水大队", "梁山大队", "邹城大队", "兖州大队", "泗水大队", "汶上大队",
				"梁山大队" };
		List<Integer> fMonth = new ArrayList<Integer>();
		List<Integer> cMonth = new ArrayList<Integer>();
		List<String> name = new ArrayList<String>();
		List<Long> id = new ArrayList<Long>();
		for (int i = 0; i < frontMonth.length; i++) {
			fMonth.add(frontMonth[i]);
		}
		for (int j = 0; j < currentMonth.length; j++) {
			cMonth.add(currentMonth[j]);
		}
		for (int k = 0; k < xName.length; k++) {
			name.add(xName[k]);
		}
		for (int l = 0; l < xId.length; l++) {
			id.add(xId[l]);
		}
		map.put("fMonth", fMonth);
		map.put("cMonth", cMonth);
		map.put("name", name);
		map.put("id", id);

		return map;

	}

}
